unit Groups;

interface

uses
  Database,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, Grids, ComCtrls, Buttons, DB, Menus, DBTables;

type
  TFormGroups = class(TForm)
    BtnAddGroup: TBitBtn;
    BtnDeleteGroup: TBitBtn;
    EditGroup: TEdit;
    Bevel2: TBevel;
    MainMenu1: TMainMenu;
    Main1: TMenuItem;
    List1: TListBox;
    BtnAdd: TSpeedButton;
    List2: TListBox;
    ComboStatus: TComboBox;
    BtnBase: TSpeedButton;
    Panel1: TPanel;
    Status: TLabel;
    procedure BtnHelpClick(Sender: TObject);
    procedure BtnAddGroupClick(Sender: TObject);
    procedure BtnDeleteGroupClick(Sender: TObject);
    procedure EditGroupKeyPress(Sender: TObject; var Key: Char);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnOKClick(Sender: TObject);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnRemoveClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnBaseClick(Sender: TObject);
    procedure BtnUpClick(Sender: TObject);
  private
    { Private declarations }
    procedure RefreshList1;
    function ValidateDeletion: string;
    function ValidateInsertion: string;
  public
    frommain: boolean;
    { Public declarations }
  end;

var
  FormGroups: TFormGroups;

implementation
uses
   Variants, DBClient;

{$R *.DFM}

procedure TFormGroups.FormCreate(Sender: TObject);
var groups: TStringList;
begin
  frommain:=true;
  groups:=TStringList.create;
  Datawell.explode(Datawell.TableOptions2.fieldbyname('Groups').asstring,',',groups);
  List2.items.assign(groups);
  RefreshList1;
  groups.free;
  ComboStatus.itemindex:=ComboStatus.items.indexof(Datawell.TableOptions.fieldbyname('FGroup').asstring);
  if ComboStatus.text = '' then
    ComboStatus.itemindex:=ComboStatus.items.indexof('BASE');
end;

procedure TFormGroups.BtnHelpClick(Sender: TObject);
begin
  Datawell.Help('groups');
end;

function TFormGroups.ValidateInsertion: string;
begin
  EditGroup.text:=Datawell.CutTrailingSpaces(EditGroup.text);
  if EditGroup.text = '' then
    result := 'You must enter a group';
  if (pos('"',EditGroup.text) > 0) or (pos(',',EditGroup.text) > 0) then
    result := 'Group names cannot contain quotes or commas';
  if not VarIsNull(datawell.TableGroups.lookup('FGroup', EditGroup.text, 'Master')) then
    result := 'That group already exists';
end;

procedure TFormGroups.BtnAddGroupClick(Sender: TObject);
var
   msg: string;
begin
  msg := ValidateInsertion;
  if msg <> '' then
  begin
    messagedlg(msg,mtinformation,[mbok],0);
    EditGroup.selectall;
    EditGroup.setfocus;
    exit;
  end;

  Datawell.TableGroups.append;
  Datawell.TableGroups.fieldbyname('Master').asinteger:=Datawell.masterinteger;
  Datawell.TableGroups.fieldbyname('FGroup').asstring:=EditGroup.text;
  Datawell.TableGroups.fieldbyname('FGColor').asinteger:=clblack;
  Datawell.TableGroups.fieldbyname('BGColor').asinteger:=clwhite;
  Datawell.TableGroups.post;
  EditGroup.text:='';
  EditGroup.setfocus;
  RefreshList1;
end;

function TFormGroups.ValidateDeletion: string;
var
   group, msg: string;
   eventt, events: boolean;
begin
  if not frommain then
    exit('You can only delete groups when this screen is opened from the main screen');
  group:='';
  if List1.itemindex >=0 then
    group:=List1.items[List1.itemindex];
  if group = '' then
    Exit('There is no group selected to delete');
  if group = 'BASE' then
    Exit('You cannot delete the BASE group');
  if List2.items.indexof(group) >= 0 then
    Exit('You cannot delete an active group');
  eventt := Datawell.TableEvents.Locate('Master;FGroup', VarArrayOf([Datawell.masterindex, group]), []) and
     (Datawell.TableEvents.fieldbyname('FEvent').asstring <> '');
  events := Datawell.TableSEvents.Locate('Master;FGroup', VarArrayOf([Datawell.masterindex, group]), []) and
     ((Datawell.TableSEvents.fieldbyname('FAction').asstring <> '') or
      (Datawell.TableSEvents.fieldbyname('Sound').asstring <> ''));
  msg:='';
  if eventt then
    msg:='The group "'+group+'" has Text Events assigned';
  if events then
    msg:='The group "'+group+'" has Status Events assigned';
  if (eventt) and (events) then
    msg:='The group "'+group+'" has Text and Status Events assigned';
  if msg <> '' then
    exit(msg+#13#10+'You must delete these events before deleting the group');
 result := '';
 end;

procedure TFormGroups.BtnDeleteGroupClick(Sender: TObject);
var
  group, mess: string;
begin
  mess := ValidateDeletion;
  if mess <> '' then
  begin
    messagedlg(mess, mtinformation,[mbok],0);
    EditGroup.selectall;
    EditGroup.setfocus;
    exit;
  end;
  group:=List1.items[List1.itemindex];
  if Datawell.TableGroups.Locate('FGroup', group, []) then
  begin
    Datawell.TableGroups.delete;
    RefreshList1;
  end;
end;

procedure TFormGroups.EditGroupKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    BtnAddGroup.click;
    key:=#0;
  end;
end;

procedure TFormGroups.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormGroups.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if (key = VK_DELETE) and (activecontrol = List1) then begin
    BtnDeleteGroup.click;
    key:=0;
  end;
end;

procedure TFormGroups.BtnOKClick(Sender: TObject);
begin
  Datawell.TableOptions.edit;
  Datawell.TableOptions.fieldbyname('FGroup').asstring:=ComboStatus.text;
  Datawell.TableOptions.post;
  Datawell.TableOptions2.edit;
  Datawell.TableOptions2.fieldbyname('Groups').asstring:=list2.items.CommaText;
  Datawell.TableOptions2.post;
  close;
end;

procedure TFormGroups.BtnAddClick(Sender: TObject);
begin
  if List2.items.count >= 10 then begin
    messagedlg('You cannot have more than 10 groups active at once',
      mtinformation,[mbok],0);
    exit;
  end;
  if List1.itemindex < 0 then exit;
  List2.items.add(List1.items[List1.itemindex]);
  List1.items.delete(List1.itemindex);
end;

procedure TFormGroups.BtnRemoveClick(Sender: TObject);
begin
  if List2.itemindex < 0 then exit;
  if List2.items[List2.itemindex] = 'BASE' then begin
    messagedlg('The BASE group cannot be removed',mtinformation,[mbok],0);
    exit;
  end;
  List2.items.delete(List2.itemindex);
  RefreshList1;
end;

procedure TFormGroups.RefreshList1;
var
  i, j: integer;
  oldstatus: string;
begin
  List1.items.clear;
  with Datawell.TableGroups do begin
    first;
    while not EOF do begin
      List1.items.add(Datawell.TableGroups.fieldbyname('FGroup').asstring);
      next;
    end;
  end;
  oldstatus:=ComboStatus.text;
  ComboStatus.items.assign(List1.items);
  ComboStatus.itemindex:=ComboStatus.items.indexof(oldstatus);
  if ComboStatus.text = '' then
    ComboStatus.itemindex:=ComboStatus.items.indexof('BASE');
  if List2.items.count < 1 then exit;
  for i:=0 to List2.items.count-1 do begin
    j:=List1.items.indexof(List2.items[i]);
    if j >= 0 then List1.items.delete(j);
  end;
  i:=List1.items.indexof('BASE');
  if i >= 0 then begin
    List1.itemindex:=i;
    BtnAdd.click;
  end;
end;

procedure TFormGroups.BtnBaseClick(Sender: TObject);
begin
  List2.items.clear;
  List2.items.add('BASE');
  ComboStatus.itemindex:=ComboStatus.items.indexof('BASE');
  RefreshList1;
end;

procedure TFormGroups.BtnUpClick(Sender: TObject);
var
  arg, i: integer;
  h: string;
begin
  arg:=(sender as TComponent).tag;
  i:=List2.itemindex;
  if i < 0 then exit;
  if (i = 0) and (arg < 0) then exit;
  if (i = (List2.items.count-1)) and (arg > 0) then exit;
  h:=List2.items[i+arg];
  List2.items[i+arg]:=List2.items[i];
  List2.items[i]:=h;
  List2.itemindex:=i+arg;
end;

end.
